package edu.ycxy.poetry.controller;


import com.github.pagehelper.PageInfo;
import edu.ycxy.poetry.entity.ShiWen;
import edu.ycxy.poetry.entity.Writer;
import edu.ycxy.poetry.mapper.ShiWenMapper;
import edu.ycxy.poetry.service.IShiWenService;
import edu.ycxy.poetry.vo.AddPoemVo;
import edu.ycxy.poetry.vo.UpdatePoemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author @xin
 * @createDate 2022/3/8
 * @Description
 */
@RestController
@RequestMapping("/poems")
@Slf4j
public class ShiWenController {
    @Autowired
    private IShiWenService shiWenService;
    @Autowired
    private ShiWenMapper shiWenMapper;

    //查询根据时间倒序的排序前42首古诗
    @RequestMapping("/findPoems")
    public List<ShiWen> findPoems(){
        List<ShiWen> shiWens = shiWenService.FindPoemsByTime();
        log.debug("获取的古诗内容:"+shiWens);
        return shiWens;
    }

    //根据诗人姓名统计诗人的诗词总数
    @RequestMapping("/{writerName}")
    public Integer CountForWriter(@PathVariable("writerName") String name){
        log.debug(name+"的诗词总数为:"+shiWenService.CountForWriter(name));
        return shiWenService.CountForWriter(name);
    }

    //根据古诗名查询古诗的整首诗
    @RequestMapping("/poemInfo/{poemName}")
    public ShiWen findPoemByName(@PathVariable("poemName") String poemName){
        log.debug("古诗的内容:"+shiWenService.findPoemByName(poemName));
        return shiWenService.findPoemByName(poemName);
    }

    //根据古诗名查询古诗作者的生平
    @RequestMapping("/writer/{poemName}")
    public Writer findWriterByPoemName(@PathVariable("poemName") String poemName){
        return shiWenService.findWriterByPoemName(poemName);
    }

    //查询古诗词内容
    @RequestMapping("/findAllPoems")
    public PageInfo<ShiWen> findAllPoems(Integer pageNum){
        //System.out.println("进入查询所有诗词的控制器方法");
        if (pageNum == null){
            pageNum = 1;
        }
        //对查询出的结果进行分页展示,每一页的数据为 10 条
        PageInfo<ShiWen> pageInfo = shiWenService.findAllPoems(pageNum,10);
        log.debug("查询出的诗词结果为:{}"+pageInfo);
        return pageInfo;
    }

    //根据古诗的id查询古诗的详细信息
    @RequestMapping("/findId/{id}")
    public ShiWen selectById(@PathVariable("id") Integer id){
        log.debug("查询出的古诗内容是:"+shiWenService.selectById(id));
        return shiWenService.selectById(id);
    }

    //根据作者姓名查询该作者的所有诗词
    @RequestMapping("/selectListByAuthor/{name}")
    public PageInfo<ShiWen> selectListByAuthor(@PathVariable("name") String name,Integer pageNum){
        if (pageNum == null){
            pageNum = 1;
        }
        PageInfo<ShiWen> pageInfo = shiWenService.selectListByAuthor(name, pageNum, 10);
        log.debug(name+"的古诗词为:"+pageInfo);
        return pageInfo;
    }

    //根据古诗的类型查询古诗的信息，<唐诗三百首> 等 , 模糊查询，使用CONCAT关键字
    @RequestMapping("/findListByTypeName/{typeName}")
    public PageInfo<ShiWen> findListByTypeName(@PathVariable("typeName") String typeName,Integer pageNum){
        if (pageNum == null){
            pageNum = 1;
        }
        PageInfo<ShiWen> pageInfo = shiWenService.findListByTypeName(typeName, pageNum, 10);
        return pageInfo;
    }

    // 添加诗词
    @RequestMapping("/addPoem")
    public String addPoem(@RequestBody AddPoemVo addPoemVo){
        String s = shiWenService.addPoem(addPoemVo);
        log.debug("获取到的值： " + s);
        if (s.equals("插入成功！！")){
            return "ok";
        }else {
            return "fail";
        }
    }

    /**
     * @BUG:根据古诗名进行详细查询无法查询到结果，但通过接口能够进行查询到结果？？？？不知道问题出在哪个地方
     * @param searchName
     * @param pageNum
     * @return
     */
    @RequestMapping("/fuzzy/search/{searchName}")
    public PageInfo<ShiWen> findListBySearchToLike(@PathVariable("searchName") String searchName,Integer pageNum){
        String name = searchName.trim();
        log.debug("获取到的诗词内容：" + shiWenMapper.findListBySearchToLike(name));
        if (pageNum == null){
            pageNum = 1;
        }
        PageInfo<ShiWen> listBySearchToLike = shiWenService.findListBySearchToLike(name, pageNum, 10);
        return listBySearchToLike;
    }

    @RequestMapping("/delete/{id}")
    public void DeletePoemById(@PathVariable("id") Integer id){
        shiWenService.deletePoemById(id);
    }

    @RequestMapping("/update")
    public Integer updatePoemById(@RequestBody UpdatePoemVo updatePoemVo){
        log.debug("获取到的数据：" + updatePoemVo);
        Integer integer = shiWenService.updatePoemById(updatePoemVo);
        if (integer == 1){
            return 1;
        }else {
            return 2;
        }
    }
}
